package com.haochang.chunkvod.controller.fragment.hostess;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseFragment;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.app.utils.VodDialog;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.model.hostess.HostessInfo;

import java.util.ArrayList;

/**
 *         created at 2016/8/27 17:03
 *         转空台页面
 */
public class DeskToNoneFragment extends BaseFragment
{
	private ArrayList<HostessInfo> allDesks = new ArrayList<HostessInfo>();
	private HostessData hostessData;
	private String deskId;
	private String deskStauus;
	private String billId;

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.fragment_desk_none, container, false);
		Button desk_to_none = (Button) view.findViewById(R.id.desk_to_none);//确定按钮
		desk_to_none.setOnClickListener(baseClickListener);
		hostessData = new HostessData((BaseActivity) getActivity());
		deskId = getArguments().getString(IntentKey.ROOM_ID);
		deskStauus = getArguments().getString(IntentKey.ROOM_STATUS);
		billId = getArguments().getString(IntentKey.BILL_ID);
		getAllDeskList();
		return view;
	}

	private OnBaseClickListener baseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			//转为空桌
			if (!TextUtils.isEmpty(deskStauus) && deskStauus.equals("using"))
			{
				if (!TextUtils.isEmpty(billId))
				{
					ToastUtils.showText(getActivity(), "账单未支付，不可转为空台");
				} else
				{
					//转为空
					toNoneDesk(0);
				}
			} else if (!TextUtils.isEmpty(deskStauus) && deskStauus.equals("paied"))
			{
				int count = getPaiedCount();
				if (count == 1)
				{
					//转为空
					toNoneDesk(1);
				} else if (count > 1)
				{
					new VodDialog.Builder(getActivity()).dialogEnum(VodDialog.DialogEnum.DOUBLE).btnNegativeText(R.string.cancel).btnNeutralText(R.string.sure)
							.cancelable(true).contentName("该桌台有多个批次状态为买单未离，是否全部转为空台？").onCustomDialogListener(new VodDialog.Builder.OnCustomDialogListener()
							{
								@Override
								public void onOnlyCancelClick()
								{

								}

								@Override
								public void onOkClick()
								{
									//转为空
									toNoneDesk(1);
								}

								@Override
								public void onCancelClick()
								{

								}
							}).build().show();
				}
			}
		}
	};

	/**
	 * 转为空台
	 * @param force 是否强制转为空 0否 1是
	 */
	private void toNoneDesk(int force)
	{
		if (!TextUtils.isEmpty(deskId))
		{
			hostessData.requestDeskToNone(deskId, force, new HostessData.NoticeListener()
			{
				@Override
				public void onRequestSuccess(String notice)
				{
					if (!TextUtils.isEmpty(notice))
					{
						getActivity().finish();
					}
				}

				@Override
				public void onFail(int errno, String errMsg)
				{

				}
			});
		}
	}

	/**
	 * 获取当前桌台上买单未离得批次数
	 * @return 返回买单未离数量
	 */
	private int getPaiedCount()
	{
		int count = 0;
		for (HostessInfo info : allDesks)
		{
			if (!TextUtils.isEmpty(deskId) && deskId.equals(info.getRoomId()) && "paied".equals(info.getRoomStatus()))
			{
				count++;
			}
		}
		return count;
	}

	/**
	 * 获取所有桌台信息
	 */
	private void getAllDeskList()
	{
		hostessData.requestDeskList("", false, new HostessData.DeskListListener()
		{

			@Override
			public void onRequestSuccess(ArrayList<HostessInfo> list)
			{
				if (!CollectionUtils.isEmpty(list))
				{
					allDesks.clear();
					allDesks.addAll(list);
				}
			}

			@Override
			public void onFinish()
			{

			}
		});
	}

}
